package entities;

import static forplay.core.ForPlay.*;

import forplay.core.GroupLayer;
import forplay.core.Image;
import forplay.core.ImageLayer;
import forplay.core.ResourceCallback;

public class Kastila {
  public static String IMAGE = "images/spanish.png";
  private ImageLayer layer;
  private float angle, x, y;
  
  public Kastila(final GroupLayer kastilaLayer, final float px, final float py) {
     
    x = px;
    y = py;
    
    Image image = assetManager().getImage(IMAGE);
    layer = graphics().createImageLayer(image);

    
    // Add a callback for when the image loads.
    // This is necessary because we can't use the width/height (to center the
    // image) until after the image has been loaded
    image.addCallback(new ResourceCallback<Image>() {
      @Override
      public void done(Image image) {
        layer.setOrigin(image.width() / 2f, image.height() / 2f);
        layer.setTranslation(x, y);
        kastilaLayer.add(layer);
      }

      @Override
      public void error(Throwable err) {
        log().error("Error loading image!", err);
      }
    });
  }

  public void update(float delta, float px, float py) {

    moveToPlayer(px, py);
   
    
    //angle += delta;
    //layer.setRotation(angle);
  }
  
  private void moveToPlayer(float player_x, float player_y)
  {
    float katipunero_x = player_x;
    float katipunero_y = player_y;
    
    // create a vector in the direction the enemy  would move 
    float dir_x = katipunero_x - this.x;
    float dir_y = katipunero_y - this.y;
    
    //normalize the direction vector
    double hyp = Math.sqrt((double)((dir_x*dir_x) + (dir_y*dir_y)));
    dir_x /= (float)hyp;
    dir_y /= (float)hyp;
    
    //add that vector to the enemy's position, multiplied by the speed you want the enemy to move:
    float speed = 2.0f;
    this.x += dir_x * speed;
    this.y += dir_y * speed;
    
    layer.setTranslation(this.x, this.y); 
  }
}
